<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;
use app\common\controller\Front;

class Pro extends Front {

	protected $beforeActionList = array(
		'setModel' => array('except' => 'category'),
	);

	public function lists()
	{
	    header("Content-type: text/html; charset=utf-8");
// 	    var_dump($this->request->param());exit;
	    $id = $this->request->param('id', 0, 'intval');
	    if (!$id) 
	    {
	        return $this->error("非法操作");
	    }
	    $styleId = $this->request->param('styleId', 0, 'intval') ?: 0;
	    if($styleId)
	    {
	        $map['p_style'] = $styleId;
	    }
	    $sceneId = $this->request->param('sceneId', 0, 'intval') ?: 0;
	    if($sceneId)
	    {
	        $map['p_scene'] = $sceneId;
	    }
	    $sizeId = $this->request->param('sizeId', 0, 'intval') ?: 0;
	    if($sizeId)
	    {
	        $map['p_size'] = $sizeId;
	    }
	    $textureId = $this->request->param('textureId', 0, 'intval') ?: 0;
	    if($textureId)
	    {
	        $map['p_texture'] = $textureId;
	    }
	    $colorId = $this->request->param('colorId', 0, 'intval') ?: 0;
	    if($colorId)
	    {
	        $map['p_color'] = $colorId;
	    }
	    $priceId = $this->request->param('priceId', 0, 'intval') ?: 0;
	    if($priceId)
	    {
	        $map['p_price'] = $priceId;
	    }
	    $tagId = $this->request->param('tagId', 0, 'intval') ?: 0;
	    if($tagId)
	    {
	        $map['p_tag'] = $tagId;
	    }
		// 搜索参数
		$data['getData'] = $this->request->param();
		// 获取产品价格属性
		$data['priceConf'] = getAttrConf(6, 'p_price');
		// 获取产品颜色属性
		$data['colorConf'] = getAttrConf(6, 'p_color');
		// 获取产品规格属性
		$data['sizeConf'] = getAttrConf(6, 'p_size');
		// 获取产品场景属性
		$data['sceneConf'] = getAttrConf(6, 'p_scene');
		// 获取产品纹理属性
		$data['textureConf'] = getAttrConf(6, 'p_texture');
		// 获取产品风格属性
		$data['styleConf'] = getAttrConf(6, 'p_style');
		// 获取产品热点属性
		$data['tagConf'] = getAttrConf(6, 'p_tag');
		// 获取父栏目
		$data['cateTree'] = getParentCategory($id);
		// 当前栏目
		$data['cate'] = $this->getCategory($id);
		// 获取子栏目
		$data['cCate'] = getChildCategory(8);
		$category = get_category_child($id);
		// 产品列表
		$map['category_id'] = array('IN', $category);
		$order = "id desc";
		$res  = model('Pro')->where($map)->order($order)->paginate(24, false,
		    [
		        'type'=>'MyPage'
		    ]);
		$list = [];
		foreach ($res as $k => $v)
		{
		    $list[$k]['id'] = $v['id'];
		    $list[$k]['title'] = $v['title'];
		    $list[$k]['category'] = get_category_name($v['category_id']);
		    $list[$k]['images'] = array_reverse(get_cover_list($v['p_covers']));
		}
		$data['list'] = $list;
		// 分页
		$data['page'] = $res->render();
		// 联系方式
		$data['contact'] = M('contact')->where(['id'=>1])->find();
		
		if ($data['cate']['template_lists']) {
		    $teamplate = 'content/' . $data['cate']['template_lists'];
		} else {
			$teamplate = 'content/list';
		}
		$this->setSeo($data['cate']['title'], $data['cate']['keywords'], $data['cate']['description']);
		$this->assign($data);
		return $this->fetch($teamplate);
	}

	public function detail($id = '', $name = '')
	{
	    header("Content-type: text/html; charset=utf-8"); 
	    // 获取内容
	    $info = $this->model->find($id);
		if (empty($info)) {
			return $this->error("无此内容！");
		}
		$data['info'] = $info;
		// 获取子栏目
		$data['cCate'] = getChildCategory(21);
		// 获取父栏目
		$data['pCate'] = getParentCategory($info['category_id']);
		// 获取当前分类
		$data['cate'] = $this->getCategory($info['category_id']);
		// 获取顶级分类
		$data['topCate'] = $this->getCategory(21);
		// 幻灯片图片
		$data['photos'] = $info['p_photos'] ? get_cover_list($info['p_photos']) : [];
		// 图集
		$data['gallery'] = $info['p_gallery'] ? get_cover_list($info['p_gallery']) : [];
		// 联系方式
		$data['contact'] = M('contact')->where(['id'=>1])->find();
		// 获取产品价格属性
		$data['priceConf'] = getAttrConf(6, 'p_price');
		// 获取产品颜色属性
		$data['colorConf'] = getAttrConf(6, 'p_color');
		// 获取产品规格属性
		$data['sizeConf'] = getAttrConf(6, 'p_size');
		// 获取产品场景属性
		$data['sceneConf'] = getAttrConf(6, 'p_scene');
		// 获取产品纹理属性
		$data['textureConf'] = getAttrConf(6, 'p_texture');
		// 获取产品风格属性
		$data['styleConf'] = getAttrConf(6, 'p_style');
		// 获取产品热点属性
		$data['tagConf'] = getAttrConf(6, 'p_tag');
		
		if (isset($info['template_detail']) && $info['template_detail']) {
			$teamplate = 'content/' . $this->modelInfo['name'] . '/' . $info['template_detail'];
		} else {
			$teamplate = 'content/' . $this->modelInfo['name'] . '/detail';
		}
		$this->assign($data);
		$title       = isset($info['title']) ? $info['title'] : '';
		$tags        = isset($info['tags']) ? $info['tags'] : '';
		$description = isset($info['description']) ? $info['description'] : '';
		$this->setSeo($title, $tags, $description);
		return $this->fetch($teamplate);
	}
	
	protected function getCategory($id) {
		$data = db('Category')->find($id);
		return $data;
	}

	protected function setModel() {
		$model_name = $this->request->param('model');
		$model_id   = $this->request->param('model_id');
		$row        = db('Model')->select();
		foreach ($row as $key => $value) {
			$name_list[$value['name']] = $value;
			$id_list[$value['id']]     = $value;
		}

		if (empty($name_list[$model_name]) && empty($id_list[$model_id])) {
			return $this->error("无此模型！");
		} else {
			$this->modelInfo = $model_name ? $name_list[$model_name] : $id_list[$model_id];
			$this->model = M($this->modelInfo['name']);

			$this->assign('model_id', $this->modelInfo['id']);
			$this->assign('model_list', $name_list);
		}
	}
}